class InstallItem < ActiveRecord::Base
  
  validates :order_id,
    :order_item_id,
    :installation_id,
    :furniture_id,
    :furniture_logistic_stat_order_item_id,
    :presence => true
    
  validates :installed_count,
    :after_serviced_count,
    :numericality => {
      :greater_than_or_equal_to => 0
    }
    
  validates :has_been_sent_to_installer_count,
    :numericality => {
      :greater_than_or_equal_to => 1
    }
  belongs_to :order
  belongs_to :order_item
  belongs_to :installation
  belongs_to :furniture
  belongs_to :furniture_logistic_stat_order_item
  belongs_to :feed
  has_many :install_items
  has_many :feeds, :dependent => :destroy  
  
  after_create do |install_item|
    install_item.update_attribute :feed, Feed::InstallItemCreated.create!(
      :order_id => install_item.order_item.order_id,
      :order_item_id => install_item.order_item_id,
      :installation_id => install_item.installation_id,
      :install_item_id => install_item.id,
      :furniture_logistic_stat_order_item_id => install_item.furniture_logistic_stat_order_item_id
    )
  end
  
  before_update do |install_item|
    self.installed_count = has_been_sent_to_installer_count - after_serviced_count if not install_item.status.has_been_sent_to_installer?
  end
  
  after_save do |install_item|    
#    install_item.order_item.reset_feed!
    
    install_item.reset_others_counts!
  end
  
  after_destroy do |install_item|
#    install_item.order_item.reset_feed!

    install_item.reset_others_counts!
  end
  
  scope :has_been_sent_to_installer, where("feeds.type = ? ", 'Feed::InstallItemCreated').includes(:feed)
  scope :installed, where('feeds.type = ?', 'Feed::InstallItemInstalled').includes(:feed)
  scope :after_serviced, where('feeds.type = ?', 'Feed::InstallItemAfterServiced').includes(:feed)
  scope :over, where('feeds.type in (?)',['Feed::InstallItemInstalled','Feed::InstallItemAfterServiced']).includes(:feed)
  
  scope :being_order_mades, where('install_items.after_serviced_count > install_items.order_made_count')
  
  def status
    ActiveSupport::StringInquirer.new(feed.code)
  end
  
#  # 重置状态feed
#  def reset_feed!
#    update_attribute :feed_id, Feed.where(:install_item_id => id).last.id
#  end
  
  def can_be_installed_count
    logger.debug "============================================="
    logger.debug "1. furniture_logistic_stat_order_item.can_be_installed_count : #{furniture_logistic_stat_order_item.can_be_installed_count}"
    logger.debug "3. installed_count : #{installed_count}"
    logger.debug "============================================="
    # ordered_count - has_been_sent_to_installer_count - installed_count + has_been_sent_to_installer_count - installed_count
    furniture_logistic_stat_order_item.can_be_installed_count + has_been_sent_to_installer_count
  end
  
  # 设置状态为已安装
  def installed!
    if status.has_been_sent_to_installer? and update_attributes(:installed_count => has_been_sent_to_installer_count)      
      update_attributes :feed => Feed::InstallItemInstalled.create!(
        :order_id => order_item.order_id,
        :order_item_id => order_item_id,
        :installation_id => installation_id,
        :install_item_id => id,
        :furniture_logistic_stat_order_item_id => furniture_logistic_stat_order_item_id
      )
    end
  end
  
  # 设置状态从 "已安装" 变为 "已派工"
  def installed_to_has_been_sent_to_installer!    
    Feed::InstallItemInstalled.where('install_item_id = ? ',id).last.destroy
    back_to_has_been_sent_to_installer!
  end
  
  # 设置状态为 售后
  def after_serviced!(attributes = {})
    if status.has_been_sent_to_installer? and update_attributes(attributes)      
      update_attributes :feed => Feed::InstallItemAfterServiced.create!(
        :order_id => order_item.order_id,
        :order_item_id => order_item_id,
        :installation_id => installation_id,
        :install_item_id => id,
        :furniture_logistic_stat_order_item_id => furniture_logistic_stat_order_item_id
      ) 
    end
  end
  
  # 设置状态从 "售后" 变为 "已派工"
  def after_serviced_to_has_been_sent_to_installer!    
    Feed::InstallItemAfterServiced.where('install_item_id = ? ',id).last.destroy
    back_to_has_been_sent_to_installer!
  end
  
  def back_to_has_been_sent_to_installer!
    update_attributes( 
      :installed_count => 0,
      :after_serviced_count => 0,
      :remark => '',
      :feed => feeds.last
    )
  end
  
  # 重置所有统计字段
  def reset_others_counts!
    furniture_logistic_stat_order_item.reset_has_been_sent_to_installer_count!
    order_item.reset_has_been_sent_to_installer_count!    
    furniture_logistic_stat_order_item.reset_installed_count!
    order_item.reset_installed_count!
  end

  # 重置订做数
  def reset_order_made_count!
    update_attribute :order_made_count, OrderMadeItem.where('order_made_items.install_item_id = ?',id).sum('order_made_items.order_made_count')
  end
  
  # 可订做数
  def can_be_order_made_count
    after_serviced_count - order_made_count
  end
  
  def last_feed
    feeds.last
  end
end
